<?php

namespace App\Console\Commands;

use App\Models\DomainList;
use App\Models\VisitRecord;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class DomainStat extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'domain:stat';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计域名访问次数';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        VisitRecord::query()->whereNotNull('domain_id')->whereDate('date', Carbon::now()->toDateString())->groupBy('domain_id')
            ->selectRaw('count(*) as use_num,domain_id')
            ->get()->each(function ($domain) {
                DomainList::query()->where('id', $domain->domain_id)->update(['use_num' => $domain->use_num]);
            });

    }
}
